<template>
  <div>
    <!--    面包屑导航区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>创建用户</el-breadcrumb-item>
    </el-breadcrumb>
<!--    单个用户添加-->
    <el-card class="bankuai">
      <el-row type="flex" justify="space-between">
        <h3>单个用户添加</h3>
        <!--        使用提示-->
        <el-col :span="5">
          <template>
            <el-popover
              placement="bottom"
              title="提示Tips："
              width="200"
              trigger="click"
              content="①若想要添加教师、管理员以及单个用户可直接双击‘添加用户’按钮实现添加。 ②若想要添加多个用户（批量创建学生账号），可通过文本框，按照’学号+空格+姓名‘，每个用户一行的格式进行批量增加学生信息。">
              <el-button icon="el-icon-star-off" type="warning" slot="reference">添加用户Tip</el-button>
            </el-popover>
          </template>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <!--        添加用户-->
        <el-col :span="8">
          <p>添加教师、管理员用户</p>
          <el-button type="primary" @click="addDialogVisible = true">添加单个用户</el-button>
        </el-col>
      </el-row>
    </el-card>
<!--    批量添加学生-->
    <el-card class="bankuai">
      <el-row type="flex" justify="space-between">
        <h3>批量用户添加</h3>
      </el-row>
      <el-row :gutter="20">
        <!--        添加用户-->
        <el-col :span="8">
          <p>可批量添加学生用户</p>
          <el-button type="primary" @click="StuDialogVisible = true">批量添加学生用户</el-button>
        </el-col>
      </el-row>
      <el-divider></el-divider>
    </el-card>
    <!--    添加单个用户的对话框-->
    <el-dialog
      title="添加单个用户"
      :visible.sync="addDialogVisible"
      width="47%" @close="addDialogClosed" class="role-mask">
      <!--      内容主体区-->
      <p class="tips"><b>补充</b>: 此窗口可实现添加单个用户（<b>学生、教师、管理员</b>）功能。<br><br> <b>注意</b>: 角色与权限需自行一一对应（<b>学生：normal、教师：manage、管理员：super</b>）,否则添加无效。</p>
      <el-form :model="addForm" status-icon :rules="addFormRules" ref="addFormRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username" placeholder="请输入用户名/学号" ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password" placeholder="请输入密码" show-password type="password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="addForm.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
<!--        <el-form-item label="角色" prop="role">-->
<!--          <el-input v-model="addForm.role"></el-input>-->
<!--        </el-form-item>-->
        <el-form-item label="角色设置" prop="role">
          <el-select v-model="addForm.role" placeholder="请选择用户角色">
            <el-option label="学生" value="student"></el-option>
            <el-option label="老师" value="teacher"></el-option>
            <el-option label="管理员" value="admin"></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="权限" prop="perms">-->
<!--          <el-input v-model="addForm.perms"></el-input>-->
<!--        </el-form-item>-->
        <el-form-item label="权限管理" prop="perms">
          <el-radio-group v-model="addForm.perms">
            <el-radio label="normal"></el-radio>
            <el-radio label="manage"></el-radio>
            <el-radio label="super"></el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <!--              底部按钮区域-->
      <span slot="footer" class="dialog-footer">
          <el-button @click="resetAdd">重 置</el-button>
        &nbsp;&nbsp;
          <el-button type="primary" @click="addUser">添 加</el-button>
      </span>
    </el-dialog>
    <!--    批量添加学生用户的对话框-->
    <el-dialog
      title="批量添加学生用户"
      :visible.sync="StuDialogVisible"
      width="50%" @close="StuDialogClosed" class="role-mask">
      <!--      内容主体区-->
      <h4>每行一名学生用户，学号、姓名用空格分开。</h4>
      <el-form :model="StuForm" :rules="StuFormRules" ref="StuFormRef">
        <el-form-item prop="Text">
          <el-input type="textarea" v-model="StuForm.Text" :autosize="{ minRows:6}" placeholder="学号1 姓名1
学号2 姓名2
... ...
... ..."></el-input>
        </el-form-item>
      </el-form>
      <!--      嵌套展现表格-->
      <el-dialog
        width="50%"
        title="批量添加学生用户"
        :visible.sync="innerVisible"
        append-to-body class="role-mask">
        <table class="tables">
          <tr>
            <th style="background-color: whitesmoke"><span style="margin: 10%; font-size: 1.2em">学 号</span><span style="font-size: 1.2em">姓 名</span></th>
          </tr>
           <tr v-for="(item,index) in this.xuehao" :key="index">
             <th>
               {{item}}
               <hr>
             </th>
           </tr>
        </table>
        <span slot="footer" class="dialog-footer">
          <el-button @click="clear">取 消</el-button>
          &nbsp;&nbsp;
          <el-button type="primary" @click="addStu">确定添加</el-button>
        </span>
      </el-dialog>
      <!--              底部按钮区域-->
      <span slot="footer" class="dialog-footer">
      <el-button @click="resetStu">重 置</el-button>
        &nbsp;&nbsp;
        <el-button type="primary" @click="addtoTable()">添 加</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  data () {
    return {
      // 控制添加单个用户对话框的显示与隐藏
      addDialogVisible: false,
      // 控制批量添加学生用户对话框的显示与隐藏
      StuDialogVisible: false,
      // 使用提示
      visible: false,
      // 嵌套
      innerVisible: false,
      // 添加单个用户的表单数据
      addForm: {
        username: '',
        password: '',
        name: '',
        role: '',
        perms: ''
      },
      addFormRules: {
        username: [
          {
            required: true, message: '请输入用户名', trigger: 'blur'
          },
          {
            min: 2, max: 15, message: '用户名长度为2-15个字符', trigger: 'blur'
          }
        ],
        password: [
          {
            required: true, message: '请输入密码', trigger: 'blur'
          },
          {
            min: 2, max: 15, message: '密码长度在6~15个字符间', trigger: 'blur'
          }
        ],
        name: [
          {
            required: true, message: '请输入姓名', trigger: 'blur'
          }
        ],
        role: [
          {
            required: true, message: '请选择角色', trigger: 'blur'
          }
        ],
        perms: [
          {
            required: true, message: '请选择权限', trigger: 'blur'
          }
        ]
      },
      // 批量添加学生用户的表单数据
      StuForm: {
        Text: ''
      },
      // 表格展示
      xuehao: ['暂无数据'],
      StuFormRules: {
        Text: [
          {
            required: true, message: '请输入要批量添加的学生信息', trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
  },
  methods: {
    // 监听添加单个用户对话框的关闭事件
    addDialogClosed () {
      this.$refs.addFormRef.resetFields()
    },
    resetAdd () {
      this.$refs.addFormRef.resetFields()
    },
    // 监听批量添加学生用户对话框的关闭事件
    StuDialogClosed () {
      this.$refs.StuFormRef.resetFields()
    },
    resetStu () {
      this.$refs.StuFormRef.resetFields()
    },
    // 点击按钮，添加新用户
    addUser () {
      this.$refs.addFormRef.validate(async valid => {
        // console.log(valid)
        if (!valid) return
        // 调用接口进行用户添加
        const { data: res } = await this.$http.post('/user/create/', this.addForm)
        if (res.status !== 200) {
          return this.$message.error()
        }
        this.$message.success('添加用户成功!')
        console.log(res)
        // 隐藏添加用户的对话框
        this.addDialogVisible = false
      })
    },
    addtoTable () {
      var table = this.StuForm.Text.split('\n')
      var a = []
      var b = []
      var i = 0
      while (table[i]) {
        a = table[i]
        b = a.split(' ')
        this.xuehao[i] = b[0] + ' ' + b[1]
        i = i + 1
        // console.log(a)
        // console.log(b)
      }
      this.innerVisible = true
    },
    // 点击按钮，批量添加学生用户
    addStu () {
      this.$refs.StuFormRef.validate(async valid => {
        // console.log(valid)
        if (!valid) return
        // 调用接口进行批量添加学生用户
        axios.defaults.headers['Content-Type'] = 'application/text;charset=utf-8'
        const { data: res } = await this.$http.post('/user/createStu/', this.StuForm.Text)
        if (res.status !== 200) {
          return this.$message.error('批量添加学生用户失败！')
        }
        this.$message.success('批量添加学生用户成功!')
        console.log(res)
        // 隐藏批量添加学生用户的对话框
        this.StuDialogVisible = false
        this.innerVisible = false
      })
    },
    // 清空数据
    clear () {
      this.innerVisible = false
      this.xuehao = ['暂无数据']
    }
  }
}
</script>

<style>
  /* 顶部下划线 */
  .role-mask .el-dialog__header{
    border-bottom: 2px solid cornflowerblue;
    padding: 20px;
  }
  .role-mask .el-dialog__title{
    font-size: 19px;
    color: #3A68C6;
  }
  .tips{
    border: #b3d4fc dashed 2px;
    border-radius: 5px;
  }
  .bankuai{
    margin-top: 2%;
  }
  .role-mask .el-dialog__body{
    padding: 5px 20px;
  }
  .tables {
    width: 80%;
    margin: 10%;
  }
</style>
